package com.newtouch.fileupload.controller;

import com.newtouch.fileupload.model.FileReq;
import com.newtouch.fileupload.model.Result;
import com.newtouch.fileupload.model.ResultCodes;
import com.newtouch.fileupload.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Slf4j
@Api(tags = "03.视频文件上传")
@RestController
@CrossOrigin
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private FileUploadService uploadFileService;

    @ApiOperation("检测视频是否已上传")
    @PostMapping("/isUpload/{traceNo}")
    public Result<Map<String,Object>> isUpload(@ApiParam(value = "回溯号") @PathVariable("traceNo") String traceNo){
        Map<String,Object> res = null;
        try {
            res = uploadFileService.findByTraceNo(traceNo);
            return Result.of(ResultCodes.SUCCESS_CODE,"",res);
        }catch (Exception e){
            log.error("上传检测失败", e);
            return Result.of(ResultCodes.ERROR_CODE,"",res);
        }
    }

    @ApiOperation("视频上传")
    @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<Map<String,Object>> uploadFile(@ApiParam("视频上传") @Validated FileReq req,
                                                 @RequestParam(value = "data", required = false) MultipartFile multipartFile){
        Map<String,Object> res = null;
        try {
            res = uploadFileService.realUpload(req,multipartFile);
        }catch (Exception e){
            log.error("视频上传失败", e);
        }
        return Result.of(res);
    }
}
